﻿using Microsoft.Extensions.Options;
using ModelDto;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SqlserverManager
{
    public class DbConnectionFactory : IDbConnectionFactory
    {
        private string writeConn = "";
        private List<string> readConnectionList = new List<string>();
        //private SqlSugarClient? context =null;
        private readonly IOptions<SystemAppSettings> SysConfig;

        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="options"></param>
        public DbConnectionFactory(IOptions<SystemAppSettings> options)
        {
            SysConfig = options;

            //初始化连接字符串
            this.writeConn = SysConfig.Value.ConnectStrWrite;
            var connectStrs = SysConfig.Value.ConnectStrReads.Split('※');
            foreach (var item in connectStrs)
            {
                if (!string.IsNullOrWhiteSpace(item))
                {
                    this.readConnectionList.Add(item);
                }
            }
        }
        /// <summary>
        /// 获取上下文操作对象 SqlSugarClient
        /// </summary>
        /// <param name="dBWriteOrRead"></param>
        /// <returns></returns>
        public SqlSugarClient CreateDbContext()
        {
            List<SlaveConnectionConfig> list = new List<SlaveConnectionConfig>();

            foreach (var item in this.readConnectionList)
            {
                list.Add(new SlaveConnectionConfig() {
                    HitRate = 10,
                    ConnectionString=item.ToString()
                });
            }


            return new SqlSugarClient(new ConnectionConfig()
            {
                ConnectionString = writeConn,
                DbType = DbType.SqlServer,
                //DbType = DbType.MySql,
                IsAutoCloseConnection = true,
                SlaveConnectionConfigs = list

            }, db =>
            {
                db.Aop.OnLogExecuting = (sql, pars) =>
                {
                    //输出sql,查看执行sql 性能无影响
                    string sqlresult = UtilMethods.GetSqlString(SqlSugar.DbType.SqlServer, sql, pars);
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine(DateTime.Now.ToString("HH:mm:ss") + "-->");
                    Console.ResetColor();
                    Console.WriteLine(sqlresult);
                    Debug.WriteLine(sqlresult);
                };
            });

        }

        ///// <summary>
        ///// 获取上下文操作对象 SqlSugarClient 【废弃】
        ///// </summary>
        ///// <param name="dBWriteOrRead"></param>
        ///// <returns></returns>
        //public SqlSugarClient CreateDbContext(DBWriteOrRead dBWriteOrRead)
        //{
        //    switch (dBWriteOrRead)
        //    {
        //        case DBWriteOrRead.Read:
        //            return new SqlSugarClient(new ConnectionConfig()
        //            {
        //                ConnectionString = GetReadConnection(),
        //                DbType = DbType.SqlServer,
        //                IsAutoCloseConnection = true

        //            }, db =>
        //            {
        //                db.Aop.OnLogExecuting = (sql, pars) =>
        //                {
        //                    //输出sql,查看执行sql 性能无影响
        //                    string sqlresult = UtilMethods.GetSqlString(SqlSugar.DbType.SqlServer, sql, pars);
        //                    Console.ForegroundColor = ConsoleColor.Red;
        //                    Console.WriteLine(DateTime.Now.ToString("HH:mm:ss") + "-->");
        //                    Console.ResetColor();
        //                    Console.WriteLine(sqlresult);
        //                    Debug.WriteLine(sqlresult);
        //                };
        //            });
        //        default:
        //            return new SqlSugarClient(new ConnectionConfig()
        //            {
        //                ConnectionString = writeConn,
        //                DbType = DbType.SqlServer,
        //                IsAutoCloseConnection = true

        //            }, db =>
        //            {
        //                db.Aop.OnLogExecuting = (sql, pars) =>
        //                {
        //                    string sqlresult = UtilMethods.GetSqlString(SqlSugar.DbType.SqlServer, sql, pars);
        //                    Console.ForegroundColor = ConsoleColor.Red;
        //                    Console.WriteLine(DateTime.Now.ToString("HH:mm:ss") + "-->");
        //                    Console.ResetColor();
        //                    Console.WriteLine(sqlresult);
        //                    Debug.WriteLine(sqlresult);
        //                };
        //            });
        //    }

        //}
        ///// <summary>
        ///// 后期放配置文件
        ///// </summary>
        ///// <returns></returns>
        //private string GetReadConnection()
        //{
        //    int i = new Random().Next(0, readConnectionList.Count);

        //    return readConnectionList[i];
        //}
    }
}
